package com.atguigu.yygh.cmn.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.yygh.cmn.listener.DictListListener;
import com.atguigu.yygh.cmn.mapper.DictMapper;
import com.atguigu.yygh.cmn.service.DictService;
import com.atguigu.yygh.model.cmn.Dict;
import com.atguigu.yygh.vo.cmn.DictEeVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {

    @Autowired
    private DictService dictService;

    // 根据id查询他的子数组有哪些
    @Override
//    @Cacheable(key = "'selectInfoList'", value = "dict")
    @Cacheable(value = "dict")
    public List<Dict> findChildData(Long id) {
        LambdaQueryWrapper<Dict> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dict::getParentId,id);
        List<Dict> list = list(lambdaQueryWrapper);

//        list.stream().map(item -> {
//            Long id1 = item.getId();
//            boolean b = this.hasChildren(id1);
//            item.setHasChildren(b);
//            return item;
//        });
        list.stream().forEach(item -> {
            Long id1 = item.getId();
            boolean b = this.hasChildren(id1);
            item.setHasChildren(b);
        });
        return list;
    }

    // 判断每一个结点是否有子数组
    private boolean hasChildren(Long id){
        LambdaQueryWrapper<Dict> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dict::getParentId,id);
        int count = count(lambdaQueryWrapper);
        return count > 0;
    }

    // 导出数据字典接口
    @Override
    public void exportdata(HttpServletResponse response) {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
//        String fileName = URLEncoder.encode("数据字典", "UTF-8");
        String fileName = "dict";
        response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");

        // 查询数据库
        List<Dict> dictlist = list(null);

        List<DictEeVo> dictEeVoList = new ArrayList<>();

        // 把dict 转为 dictEeVo
//        BeanUtils.copyProperties(dictlist,dictEeVoList);
        dictlist.stream().forEach(item -> {
            DictEeVo dictEeVo = new DictEeVo();
            BeanUtils.copyProperties(item,dictEeVo);
            dictEeVoList.add(dictEeVo);
        });

        // 写操作调用方法写参数
        try {
            EasyExcel.write(response.getOutputStream(), DictEeVo.class).sheet("dict")
                    .doWrite(dictEeVoList);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    // 导入数据字典  这个就是先删除缓存 再放入
    @Override
    @CacheEvict(value = "dict", allEntries = true)
    public void importData(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(),DictEeVo.class,new DictListListener(dictService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
